package main

import (
	"fmt"
	"io"
	"net"
	"net/http"
	"net/rpc"
)

func main() {
	http.HandleFunc("/hello", hello)
	//静态资源列表
	// http.Handle("/static",http.StripPrefix("/static",http.FileServer(http.Dir("static"))))
	http.ListenAndServe(":9999", nil)

	//RPC服务示例
	//go server()
	//go client()
	//
	//var input string
	//fmt.Scanln(&input)
}

//简单服务
func hello(res http.ResponseWriter, req *http.Request) {
	res.Header().Set("Content-Type", "text/html")
	io.WriteString(res, "<doctype html><html><head><title>Hello World</title></head><body>Hello World!</body></html>")
}

type Server struct {
}

func (this *Server) Negate(i int64, reply *int64) error {
	*reply = -i
	return nil
}

//服务provider
func server() {
	//注册服务
	rpc.Register(new(Server))
	//启动监听
	listen, err := net.Listen("tcp", ":8888")
	if nil != err {
		fmt.Println(err)
		return
	}
	for {
		con, err := listen.Accept()
		if nil != err {
			continue
		}
		go rpc.ServeConn(con)

	}
}

//服务consumer
func client() {
	//连接服务
	con, err := rpc.Dial("tcp", "127.0.0.1:8888")
	if nil != err {
		fmt.Println(err)
		return
	}

	var result int64
	//调用rpc接口
	err = con.Call("Server.Negate", int64(999), &result)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("Server.Negate:", result)
	}

}
